<?php 
/**
*	接口
*/
class indexClassAction extends apiAction
{

	
	/**
	*	修改密码
	*/
	public function editpassAction()
	{
		$passoldPost 	= $this->post('passoldPost');
		$passwordPost 	= $this->post('passwordPost');
		$password1Post 	= $this->post('password1Post');
		
		if(isempt($passwordPost) || isempt($passoldPost))return returnerror('无效输入');
		
		$rand 	= $this->adminrs['rand'];
		if(!isempt($rand))$passoldPost.=$rand;
		if(md5($passoldPost) != $this->adminrs['pass'])return returnerror('旧密码错误');
		
		$rand 	= 'edit'.rand(10000,99999).'';
		m('users')->update(array(
			'rand' => $rand,
			'pass' => md5($passwordPost.$rand)
		),$this->adminid);
		
		$arows = m('usera')->getall('`uid`='.$this->adminid.'');
		foreach($arows as $k=>$ars){
			m('hdiao')->send($ars['cid'],'editpass','', array(
				'user' 	=> $ars['user'],
				'pass'	=> $passwordPost,
			));
		}
		
		return returnsuccess('修改成功');
	}
	
	/**
	*	修改头像
	*/
	public function editfaceAction()
	{
		$fid = (int)$this->get('fid','0');
		$uid = (int)$this->get('uid', $this->adminid);
		$frs = m('file')->getone($fid);
		if(!$frs)return returnerror('无法修改');
		$path= $frs['thumbpath'];
		if(isempt($path))$path = $frs['filepath'];
		$fileext = $frs['fileext'];
		$npath = str_replace('.'.$fileext.'','face.'.$fileext.'',$path);
		copy($path, $npath);
		if(!file_exists($npath))return returnerror('无法修改1');
		m('users')->update(array('face' => $npath), $uid);
		
		return returnsuccess(array(
			'msg' => '头像修改成功',
			'face'=> m('users')->getface($npath)
		));
	}
	
	/**
	*	快捷登录二维码
	*/
	public function loginewmAction()
	{
		header("Content-type:image/png");
		$urls= getconfig('outurl',URL);
		$user= $this->adminrs['user'];
		c('cache')->set('login'.$user.'', $this->adminid, 300);
		$pass= md5($this->adminrs['pass']);
		$url = ''.$urls.'?user='.$this->jm->base64encode($user).'&d=we&pass='.$pass.'';
		$img = c('qrcode')->show($url);
		echo $img;
	}
	
	/**
	*	个人资料修改
	*/
	public function editinfoAction()
	{
		$value 	= trim($this->post('value'));
		$fields = $this->post('fields');
		$farr 	= array('name','mobile','user','email');
		if(isempt($value))return returnerror('notvalue');
		if(!in_array($fields, $farr))return returnerror('notaccess');
		$db 	= m('users');
		$ch 	= c('check');
		if($fields=='user'){
			if($ch->isincn($value))return returnerror('不能有中文');
		}
		if($fields=='mobile'){
			if(!$ch->iscnmobile($value))return returnerror('手机号格式有误');
		}
		if($fields=='email'){
			if(!$ch->isemail($value))return returnerror('邮箱格式有误');
		}
		if($fields!='name'){
			$to = $db->rows("`$fields`='$value' and `id`<>'{$this->adminid}'");
			if($to>0)return returnerror('“'.$value.'”已存在');
		}
		$db->update("`$fields`='$value'", $this->adminid);
		return returnsuccess();
	}
	
	/**
	*	修改手机号
	*/
	public function editmobileAction()
	{
		$device 	= $this->post('device');
		$mobile 	= trim($this->post('mobile'));
		$mobilecode = trim($this->post('mobilecode'));
		$code 		= trim($this->post('smscode'));
		$db   = m('users');
		
		if(!$device || !$code)return returnerror('error');
		if(isempt($mobile))return returnerror('手机号不能为空');
		
		$urs = $db->getone("`mobile`='$mobile'");
		if($urs)return returnerror('手机号已使用过了');
		
		if(!c('sms')->checkcode($mobile, $code, 'edit', $device))return returnerror('手机短信验证码错误');

		$db->update("`mobile`='$mobile'", $this->adminid);
		m('usera')->update("`mobile`='$mobile'", '`uid`='.$this->adminid.'');
		$arows = m('usera')->getall('`uid`='.$this->adminid.'');
		foreach($arows as $k=>$ars){
			m('hdiao')->send($ars['cid'],'editmobile','', array(
				'user' 		=> $ars['user'],
				'mobile'	=> $ars['mobile'],
				'mobilecode'=> $ars['mobilecode'],
			));
		}
		return returnsuccess();
	}
}